﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;

namespace KqAutoDesign.SonViews
{
    /// <summary>
    /// DataGrid列只能输入数字
    /// </summary>
    public class NumericTextColumn : DataGridTextColumn
    {
        /// <summary>
        /// 是否是整数
        /// </summary>
        public bool IsInteger
        {
            get { return (bool)GetValue(IsIntegerProperty); }
            set { SetValue(IsIntegerProperty, value); }
        }
        public static readonly DependencyProperty IsIntegerProperty =
            DependencyProperty.Register("IsInteger", typeof(bool), typeof(NumericTextColumn), new PropertyMetadata(true));

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            var edit = editingElement as TextBox;
            edit.SetValue(InputMethod.IsInputMethodEnabledProperty, false);

            edit.PreviewTextInput -= Edit_PreviewTextInput;
            edit.PreviewKeyDown -= KeyDownInput;

            edit.PreviewTextInput += Edit_PreviewTextInput;
            edit.PreviewKeyDown += KeyDownInput;

            //禁用复制粘贴功能
            DataObject.AddPastingHandler(edit, OnPaste);
            edit.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, CommandCanExecute));
            edit.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, CommandCanExecute));
            edit.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, CommandCanExecute));
            edit.ContextMenu = null;

            return base.PrepareCellForEdit(editingElement, editingEventArgs);
        }

        private void CommandCanExecute(object sender, ExecutedRoutedEventArgs e)
        {
            e.Handled = true;
        }

        private static void KeyDownInput(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void OnPaste(object sender, DataObjectPastingEventArgs e)
        {
            var data = e.SourceDataObject.GetData(DataFormats.Text);
            if (!IsDataValid(data)) e.CancelCommand();
        }

        private void Edit_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (sender is TextBox)
            {
                if (IsInteger)
                {
                    e.Handled = !IsDataValid(e.Text);
                }
                else
                {
                    TextBox textBox = (sender as TextBox);
                    Regex numbeRegex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
                    e.Handled =
                            !numbeRegex.IsMatch(
                                textBox.Text.Insert(
                                    textBox.SelectionStart, e.Text));
                    textBox.Text = textBox.Text.Trim();
                }

            }
        }

        bool IsDataValid(object data)
        {
            try
            {
                Convert.ToDouble(data);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
